#include "app_main.h"
#include "app_settings.h"
#include <stdio.h>

#if (LV_USE_FS_WIN32 == 1)
#include "../hal/hal_link.h"
#elif (LV_USE_FS_POSIX == 1)
#include "hal_link.h"
#endif

static struct {
    char *mode_pwd;
    char *mode_name;
} mode[] = {
    {.mode_pwd = "A:./img/uav_mode.png", .mode_name = "uav mode"},
    {.mode_pwd = "A:./img/ppm_mode.png", .mode_name = "ppm mode"},
    {.mode_pwd = "A:./img/usb_mode.png", .mode_name = "usb mode"},
    {.mode_pwd = "A:./img/lrj_mode.png", .mode_name = "Deer mode"},
};

lv_obj_t *img_mode;
lv_obj_t *lable_mode;
lv_obj_t *bt_fun;
lv_obj_t * rocker[4];
lv_obj_t *sw_bt;

// 全局变量，存储主界面对象
static lv_obj_t *main_screen;

uint8_t main_show_mode_num;

// 电压值
static double voltage = 3.7;
lv_obj_t *cont_mode;
lv_obj_t *remote_info;
lv_obj_t *switch_info;
lv_obj_t *opencv_info;

// 设置按钮点击事件处理函数
static void settings_button_event_cb(lv_event_t *e) {
    lv_obj_t *btn = lv_event_get_target(e);
    (void)btn;

    hal_link->buzz_di();

    // 以淡入动画效果切换到设置界面
   lv_scr_load_anim(settings_screen_get(), LV_SCR_LOAD_ANIM_FADE_ON, animation_time, 0, false);
}

/**
 * @brief  显示电压
 * @param  void
 * @retval void
 */
static void show_adc(void)
{
    // 显示电压
    lv_obj_t *voltage_label = lv_label_create(main_screen);
    char voltage_str[20];
    snprintf(voltage_str, sizeof(voltage_str), "Voltage: %.2f V", voltage);
    lv_label_set_text(voltage_label, voltage_str);

    // lv_obj_set_size(voltage_label, 200, 50);
    lv_obj_align_to(voltage_label, main_screen, LV_ALIGN_BOTTOM_LEFT, 20, -30);
}

void set_show_mode(uint8_t show_mode_num)
{
    if (!is_screen_active(main_screen)) {
        return;
    }

    if (show_mode_num >= 4) {
        printf("show_mode_num error:%d\n", show_mode_num);
        return;
    }

    main_show_mode_num = show_mode_num;

    lv_img_set_src(img_mode, mode[show_mode_num].mode_pwd);
    lv_label_set_text_static(lable_mode, mode[show_mode_num].mode_name);
}

uint8_t get_show_mode(void)
{
    return main_show_mode_num;
}

static void show_mode(void)
{
    // 创建容器（框框）
    cont_mode = lv_obj_create(main_screen);
    lv_obj_set_size(cont_mode, 300, 280);
    lv_obj_align(cont_mode, LV_ALIGN_TOP_LEFT, 14, 10);
    // 去除容器的滚动条
    // lv_obj_set_scrollbar_mode(cont_mode, LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(cont_mode, LV_OBJ_FLAG_SCROLLABLE);

    // 显示图片
    img_mode = lv_img_create(cont_mode);
    lv_obj_set_size(img_mode, 200, 200);
    lv_obj_align(img_mode, LV_ALIGN_CENTER, 0, -20);

    lable_mode = lv_label_create(cont_mode);
    lv_obj_align_to(lable_mode, img_mode, LV_ALIGN_OUT_BOTTOM_MID, -50, 5);

    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);

    // 设置标签文本颜色为白色
    lv_style_set_text_color(&style_label, lv_color_black());
    lv_style_set_text_font(&style_label, &lv_font_montserrat_26);
    lv_obj_add_style(lable_mode, &style_label, 0);
}

void set_remote_info(uint8_t * rock)
{
    if (!is_screen_active(main_screen)) {
        return;
    }

    for (int i=0; i < 4; i++) {
        if (rock[i] <= 100) {
            lv_slider_set_value(rocker[i], rock[i]-50, LV_ANIM_OFF);
        }
    }

    if (rock[5] > 50) {
        lv_obj_align(sw_bt, LV_ALIGN_LEFT_MID, 3, 11);
        lv_obj_set_style_bg_color(sw_bt, lv_palette_main(LV_PALETTE_GREEN), 0);
    } else if (rock[5] == 50) {
        lv_obj_align(sw_bt, LV_ALIGN_CENTER, -10, 11);
        lv_obj_set_style_bg_color(sw_bt, lv_palette_main(LV_PALETTE_BLUE), 0);
    } else {
        lv_obj_align(sw_bt, LV_ALIGN_RIGHT_MID, -20, 11);
        lv_obj_set_style_bg_color(sw_bt, lv_palette_main(LV_PALETTE_YELLOW), 0);
    }

    for (int i = 0; i < 1; i++)
    {
        if (rock[6] > 50) {
            lv_obj_add_state(bt_fun, LV_STATE_CHECKED);
        } else {
            lv_obj_clear_state(bt_fun, LV_STATE_CHECKED); 
        }
    }
    
}

static void show_remote_info(void)
{
    // 创建容器（框框）
    remote_info = lv_obj_create(main_screen);
    lv_obj_set_size(remote_info, 140, 140);
    lv_obj_align(remote_info, LV_ALIGN_TOP_LEFT, 10, 10);
    // 去除容器的滚动条
    // lv_obj_set_scrollbar_mode(remote_info, LV_SCROLLBAR_MODE_OFF);
    lv_obj_clear_flag(remote_info, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_align_to(remote_info, cont_mode, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 10);

    /*摇杆反馈*/
    static lv_style_t rocker_sty;
    lv_style_init(&rocker_sty);

    lv_style_set_width(&rocker_sty, 10);
    lv_style_set_height(&rocker_sty, 120);

    // 创建样式对象
    static lv_style_t style_indicator;
    static lv_style_t style_knob;

    // 初始化样式
    lv_style_init(&style_indicator);
    lv_style_init(&style_knob);
     
    // 设置指示器样式
    lv_style_set_bg_color(&style_indicator, lv_color_hex(0x007bff)); // 指示器背景颜色
    lv_style_set_bg_opa(&style_indicator, LV_OPA_COVER);
    lv_style_set_radius(&style_indicator, LV_RADIUS_CIRCLE); // 指示器圆角
    lv_style_set_size(&style_indicator, 10);

    // 设置旋钮样式
    lv_style_set_bg_color(&style_knob, lv_color_hex(0xffffff)); // 旋钮背景颜色
    lv_style_set_bg_opa(&style_knob, LV_OPA_COVER);
    lv_style_set_radius(&style_knob, LV_RADIUS_CIRCLE); // 旋钮圆角
    lv_style_set_border_color(&style_knob, lv_color_hex(0x007bff)); // 旋钮边框颜色
    lv_style_set_border_width(&style_knob, 2); // 旋钮边框宽度
    lv_style_set_shadow_width(&style_knob, 4); // 旋钮阴影宽度
    lv_style_set_shadow_color(&style_knob, lv_color_hex(0x888888)); // 旋钮阴影颜色
    lv_style_set_shadow_ofs_y(&style_knob, 2); // 旋钮阴影偏移量
    lv_style_set_size(&style_knob, 16); // 设置旋钮的大小

    lv_obj_t *last;
    for (int i = 0; i < 4; i++)
    {
        rocker[i] = lv_slider_create(remote_info);
        lv_obj_add_style(rocker[i], &rocker_sty, 0);  /*添加样式*/
        lv_slider_set_range(rocker[i], -50, 50);      /*添加范围*/
        lv_slider_set_mode(rocker[i], LV_SLIDER_MODE_SYMMETRICAL);    /*设置模式*/

        lv_obj_clear_flag(rocker[i], LV_OBJ_FLAG_CLICKABLE);

        lv_obj_add_style(rocker[i], &style_indicator, LV_PART_INDICATOR);
        lv_obj_add_style(rocker[i], &style_knob, LV_PART_KNOB);

        if (i == 0) {
            lv_obj_align(rocker[i], LV_ALIGN_LEFT_MID, 0, 0);
        } else {
            lv_obj_align_to(rocker[i], last, LV_ALIGN_OUT_RIGHT_MID, 20, 0);
        }
        
        last = rocker[i];
    }
}

static void show_switch_info(void)
{
    // 创建容器（框框）
    switch_info = lv_obj_create(main_screen);
    lv_obj_set_size(switch_info, 140, 140);
    lv_obj_align(switch_info, LV_ALIGN_TOP_LEFT, 10, 10);
    // 去除容器的滚动条
    lv_obj_set_scrollbar_mode(switch_info, LV_SCROLLBAR_MODE_OFF);

    lv_obj_align_to(switch_info, cont_mode, LV_ALIGN_OUT_BOTTOM_RIGHT, 0, 10);

    bt_fun = lv_switch_create(switch_info);
    // 清除按钮的可点击和可聚焦标志
    lv_obj_clear_flag(bt_fun, LV_OBJ_FLAG_CLICKABLE);

    lv_obj_t *div = lv_obj_create(switch_info);;
    lv_obj_set_size(div, 80, 20);
    lv_obj_clear_flag(div, LV_OBJ_FLAG_SCROLLABLE);
    lv_obj_align_to(div, bt_fun, LV_ALIGN_OUT_BOTTOM_LEFT, 0, 20);

    sw_bt =  lv_obj_create(switch_info);

    // 设置对象的大小
    lv_obj_set_size(sw_bt, 25, 25);
    // 设置对象的位置
    lv_obj_align_to(sw_bt, div, LV_ALIGN_CENTER, 0, 0);
    
    // 创建一个样式
    static lv_style_t style;
    lv_style_init(&style);
    lv_style_set_radius(&style, LV_RADIUS_CIRCLE); // 设置圆角为圆形
    lv_obj_set_style_bg_color(sw_bt, lv_palette_main(LV_PALETTE_BLUE), 0);

    // 将样式应用到对象上
    lv_obj_add_style(sw_bt, &style, 0);
}
static void show_opencv_info(void)
{
    // 创建容器（框框）
    opencv_info = lv_obj_create(main_screen);
    lv_obj_set_size(opencv_info, 560, 430);
    lv_obj_align(opencv_info, LV_ALIGN_TOP_LEFT, 10, 10);
    // 去除容器的滚动条
    lv_obj_clear_flag(opencv_info, LV_OBJ_FLAG_SCROLLABLE);

    lv_obj_align_to(opencv_info, cont_mode, LV_ALIGN_OUT_RIGHT_TOP, 10, 0);

    // 显示图片
    lv_obj_t *img = lv_img_create(opencv_info);
    lv_obj_set_size(img, 560, 430);
    lv_img_set_src(img, "A:./img/opencv_error.jpg");
    lv_obj_align(img, LV_ALIGN_CENTER, 0, 0);
}

static void show_set_button(void)
{
    // 设置按钮
    lv_obj_t *settings_button = lv_btn_create(main_screen);
    lv_obj_set_size(settings_button, 200, 50);
    lv_obj_align_to(settings_button, cont_mode, LV_ALIGN_BOTTOM_RIGHT, -20, -20);
    lv_obj_add_event_cb(settings_button, settings_button_event_cb, LV_EVENT_CLICKED, NULL);

    lv_obj_t *settings_label = lv_label_create(settings_button);
    lv_label_set_text(settings_label, "设置");

    // 初始化标签样式
    static lv_style_t style_label;
    lv_style_init(&style_label);

    // 设置标签文本颜色为白色
    lv_style_set_text_color(&style_label, lv_color_white());
    lv_style_set_text_font(&style_label, &font_22_alibaba);
    lv_obj_add_style(settings_label, &style_label, 0);
}

/**
 * @brief  创建主界面
 * @param  void
 * @retval void
 */
void main_screen_init(void)
{
    main_screen = lv_obj_create(NULL);

    show_set_button();

    /* 先后顺序不能改，因为先后创建的问题 */
    show_mode();

    show_remote_info();

    show_switch_info();

    show_opencv_info();

    show_adc();

    // 加载主界面
    lv_scr_load(main_screen);
}


// 获取 About OS 界面对象
lv_obj_t *main_screen_get(void) 
{
    return main_screen;
}
/******************* (C) COPYRIGHT 2025 FUYEBING*****END OF FILE************/